package com.lq.ecoldchain.ui.mymileage.details

import android.arch.lifecycle.MutableLiveData
import com.lq.ecoldchain.base.viewmodel.BaseViewModel
import com.lq.ecoldchain.entity.MileageDetailBean
import com.lq.ecoldchain.internet.BaseObserver
import com.lq.ecoldchain.util.toast

/*
*2019/1/5 0005  20:03
*function by lq
*/
class MileageDetailViewModel : BaseViewModel() {
    private var page = 1
    private val limit = 10
    val refreshing by lazy { MutableLiveData<Boolean>() }
    val list by lazy { ArrayList<MileageDetailBean>() }
    val adapter by lazy { MileageDetailAdapter(list) }

    init {
        mileageDetail()
    }

    fun refreshData() {
        refreshing.value = true
        mileageDetail()
    }

    private fun mileageDetail() {
        BaseNetCore.ApiServiceImpl.mileageDetail(page, limit).compose(RxTransformer.switchSchedulers()).subscribe(object : BaseObserver<List<MileageDetailBean>>() {
            override fun onSuccess(t: List<MileageDetailBean>?) {
               list.clear()
                if(!t.isNullOrEmpty())
                    list.addAll(t)
                adapter.notifyDataSetChanged()
                if (refreshing.value == true)
                    toast { "刷新成功" }
                refreshing.value = false
            }

            override fun onFail(code: String) {
                refreshing.value = false
            }
        })
    }
}